package com.hy.hywebview

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import com.hy.hybase.loadsir.ErrorCallback
import com.hy.hybase.loadsir.LoadingCallback
import com.hy.hywebview.databinding.FragmentHyWebviewBinding
import com.hy.hywebview.webviewprocess.webviewclient.WebViewCallBack
import com.kingja.loadsir.core.LoadService
import com.kingja.loadsir.core.LoadSir
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshListener

/**
 * @Description:
 * @Author: wdk
 * @CreateTime: 2024/4/13 15:50
 */
class HYWebViewFragment : Fragment(), WebViewCallBack, OnRefreshListener {

    var mUrl: String = ""
    var mCanNativeRefresh = true
    private var mIsError = false
    private lateinit var mBinding: FragmentHyWebviewBinding
    private lateinit var mLoadService: LoadService<*>

    companion object {
        fun newInstance(url: String, canNativeRefresh: Boolean): Fragment {
            val args = Bundle()
            args.putString(HYFragmentConfig.HY_WEBVIEW_FRAGMENT_URL, url)
            args.putBoolean(
                HYFragmentConfig.HY_WEBVIEW_FRAGMENT_CAN_NATIVE_REFRESH,
                canNativeRefresh
            )

            val fragment = HYWebViewFragment()
            fragment.arguments = args
            return fragment
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val bundle = arguments
        bundle?.also {
            mUrl = it.getString(HYFragmentConfig.HY_WEBVIEW_FRAGMENT_URL) ?: ""
            mCanNativeRefresh =
                it.getBoolean(HYFragmentConfig.HY_WEBVIEW_FRAGMENT_CAN_NATIVE_REFRESH, true)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        mBinding = FragmentHyWebviewBinding.inflate(inflater)
        return mBinding.root
    }


    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        mBinding.hyWebView.registerWebViewCallback(this)
        mBinding.hyWebView.loadUrl(mUrl)

        //添加布局状态view
        mLoadService = LoadSir.getDefault().register(mBinding.smartRefreshLayout).apply {
            showCallback(LoadingCallback::class.java)
            mBinding.hyWebView.reload()
        }

        mBinding.smartRefreshLayout.apply {
            setOnRefreshListener(this@HYWebViewFragment)
            setEnableRefresh(mCanNativeRefresh)
            setEnableLoadMore(false)
        }
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        mBinding.hyWebView.reload()
    }

    override fun pageStarted(url: String?) {
        mLoadService.showCallback(LoadingCallback::class.java)
    }

    override fun pageFinished(url: String?) {
        if (mIsError) {
            mBinding.smartRefreshLayout.setEnableRefresh(true)
        } else {
            mBinding.smartRefreshLayout.setEnableRefresh(mCanNativeRefresh)
        }
        mBinding.smartRefreshLayout.finishRefresh()

        if (mIsError) {
            mLoadService.showCallback(ErrorCallback::class.java)
        } else {
            mLoadService.showSuccess()
        }
        mIsError = false
    }

    override fun onError() {
        mIsError = true
        mBinding.smartRefreshLayout.finishRefresh()
    }

    override fun updateTitle(title: String) {
        val activity = requireActivity()
        if (activity is HYWebViewActivity) {
            activity.updateTitle(title)
        }
    }


}